import android.util.Log;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
public class Parse {
static InputStream is = null;
static JSONObject jObj = null;
static String json = null;
public Parse(){
}
public static String getJSONFromUrl(String url) throws IOException {
Log.e("URL",url);
try{
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}catch (UnsupportedEncodingException e){
Log.e("JSON Parser", "UnsupportedEncodingException " + e.toString());
e.printStackTrace();
}catch (ClientProtocolException e){
Log.e("JSON Parser", "ClientProtocolException " + e.toString());
e.printStackTrace();
}catch (IOException e){
Log.e("JSON Parser", "IOException " + e.toString());
e.printStackTrace();
}
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while((line = reader.readLine())!= null){
sb.append(line + "\n");
}
is.close();
json = sb.toString();
}catch (Exception e){
Log.e("Buffer Error", "Error converting result " + e.toString());
}
return json;
}
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
public class Havadurumu extends AppCompatActivity {
TextView txt_derece;
Button btn_goster;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_havadurumu);
txt_derece = (TextView) findViewById(R.id.txtgoster);
btn_goster = (Button) findViewById(R.id.btn_hava);
btn_goster.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new getWeatherCondition().execute();
}
});
}
private class getWeatherCondition extends AsyncTask<Void,Void,Void>{
String dolar;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... Params) {
String url = "http://www.doviz.gen.tr/doviz_json.asp?version=1.0.4";
JSONObject jsonObject = null;
try {
String json = Parse.getJSONFromUrl(url);
try {
jsonObject = new JSONObject(json);
} catch (JSONException e) {
e.printStackTrace();
}
try {
dolar = jsonObject.getString("sonkayit");
} catch (JSONException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void args) {
txt_derece.setText(dolar);
}
}
}
Merhaba arkadaşlar böyle bir şeyler çıktı ortaya çalışıyor ama sizce ne kadar verimlidir ve hızlıdır. Tavsiyelerinizi bekliyorum